﻿<cfscript>
/** 
* String Format Frameworks
* 
* @hint "String Format Frameworks" 
*/ 

component 
	displayname="public.service.util.stringAdvice" 
	output=false 
	{
	
	/**
	* @hint "初始化对象"
	*/
	public public.service.util.stringAdvice function init() output=false {
		return this;
	}
	
	/**
	* @hint "电子邮件地址宽松验证"
	* 
	* @fString "字符串"
	*/
	public boolean function isEmail( string fString="" ) output=false {
		
		if ( refind("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", arguments.fstring) ) {
			return true;
		}
		
		return false;
	}
	
	/**
	* @hint "HTML过滤编码"
	* 
	* @fString "字符串"
	*/
	public string function HTMLEncode( string fString="" ) output=false {
		
		var result = Rtrim( arguments.fString );
		
		result = replaceNoCase( result, ">", "&gt;", "all" );
		result = replaceNoCase( result, "<", "&lt;", "all" );
		result = replaceNoCase( result, CHR(32), " ", "all" );		//&nbsp;
		result = replaceNoCase( result, CHR(9), " ", "all" );			//&nbsp;
		result = replaceNoCase( result, CHR(34), "&quot;", "all" );
		result = replaceNoCase( result, CHR(39), "&##39;", "all" );	//单引号过滤
		result = replaceNoCase( result, CHR(13), "", "all" );
		result = replaceNoCase( result, CHR(10) & CHR(10), "</p><p> ", "all" );
		result = replaceNoCase( result, CHR(10), "<br/> ", "all" );
		
		return result;
	}

	/**
	* @hint "HTML FORM过滤编码"
	* 
	* @fString "字符串"
	*/
	public string function HTMLFormEncode( string fString="" ) output=false {
		
		var result = Rtrim( arguments.fString );
		
		result = replaceNoCase( result, ">", "&gt;", "all" );
		result = replaceNoCase( result, "<", "&lt;", "all" );
		result = replaceNoCase( result, CHR(32), " ", "all" );		//&nbsp;
		result = replaceNoCase( result, CHR(9), " ", "all" );			//&nbsp;
		result = replaceNoCase( result, CHR(34), "&quot;", "all" );
		result = replaceNoCase( result, CHR(39), "&##39;", "all" );	//单引号过滤
		
		return result;
	}
	
	/**
	* @hint "过滤HTML标签"
	* 
	* @fString "字符串"
	*/
	public string function XHTMLSafeFormat( string fString="" ) output=false {
		
		var result = reReplaceNocase( arguments.fString, "<(.[^>]*)>", "", "all" );
		
		result = replaceNoCase( result, ">", "&gt;", "all" );
		result = replaceNoCase( result, "<", "&lt;", "all" );

		return result;
	}
	
	/**
	* @hint "将指定字符串格式化为符合XHTML要求的<p></p>标签封装段落"
	* 
	* @fString "字符串"
	*/
	public string function XHTMLParagraphFormat( string fString="" ) output=false {
		
		var result = reReplaceNocase(arguments.fString, "<(.[^>]*)>", "","all");
		
		result = replaceNoCase( result, ">", "&gt;", "all" );
		result = replaceNoCase( result, "<", "&lt;", "all" );

		if(Len(RTrim(result))) {
			result = "<p>" & Replace(arguments.fString, CHR(13) & CHR(10), "</p>" & CHR(13) & CHR(10) & "<p>", "ALL") & "</p>";
		}
		return result;
	}
	
	/**
	* @hint "将电子表格中的回车空格等符号去除"
	* 
	* @fString "字符串"
	*/
	public string function trimExcel( string fString="" ) output=false {
		
		var result = trim( arguments.fString );
		
		result = replace( result, chr(13), "", "ALL");
		result = replace( result, chr(10), "", "ALL");
		
		return result;
	}

	/**
	* @hint "将英文单词进行首字大写"
	* 
	* @fString "字符串"
	* @caseSensitive "是否区分大小写"
	*/
	public string function capitalizeString( string fString="", boolean caseSensitive=true ) output=false {
		
		if ( arguments.caseSensitive ) {
			return ReReplace(str,"\b(\w)","\u\1","ALL");
		}
		
		return ReReplaceNoCase(str,"\b(\w)","\u\1","ALL");
	}

	/**
	* @hint "将阿拉伯数字转换为中文数字"
	* 
	* @fNumChar "字符"
	*/
	public string function ChineseNumber( string fNumChar="" ) output=false {
		
		switch ( arguments.fNumChar ) {
			case "1": return "一";
			case "2": return "二";
			case "3": return "三";
			case "4": return "四";
			case "5": return "五";
			case "6": return "六";
			case "7": return "七";
			case "8": return "八";
			case "9": return "九";
			case "0": return "零";
		}
		
		return "";
	}

}
</cfscript>